package com.cgnpc.scp.achievement.controller;

import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.achievement.mybatis.entity.Capacity;
import com.cgnpc.scp.achievement.pojo.CapacityDetail;
import com.cgnpc.scp.achievement.pojo.IdReq;
import com.cgnpc.scp.achievement.pojo.SaveCapacityReq;
import com.cgnpc.scp.achievement.service.CapacityService;
import com.cgnpc.scp.common.bean.CreateFileInfo;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Locale;

@Controller
@RequestMapping("/scp/achievement/capacity")
public class CapacityController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(CapacityController.class);

    @Autowired
    private MessageSource messageSource;


    @Autowired
    private CapacityService capacityService;

    @Autowired
    private CurrentUser currentUser;
    /**********************************
     * 用途说明: 保存能力建设
     * 参数说明 Capacity
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="保存能力建设")
    @UBA(module = "成果管理", action = "保存能力建设", channel = OperatorType.Button)
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult save(HttpServletRequest request, @RequestBody SaveCapacityReq req) {
        CapacityDetail result = capacityService.save(req);
        if (result != null)
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,result);
        else
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,"保存失败!");
    }

    /**********************************
     * 用途说明: 根据ID 查询能力建设 详细信息
     * 参数说明 id
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="根据ID 查询能力建设 详细信息")
    @UBA(module = "成果管理", action = "查询能力建设 详细信息", channel = OperatorType.Button)
    @RequestMapping(value = "/getInfoById", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult getInfoById(HttpServletRequest request, @RequestBody IdReq req) {
        CapacityDetail result = capacityService.getById(req.getId());
        if (result != null)
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,result);
        else
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,"查询失败!");
    }


    /**********************************
     * 用途说明: 查询能力建设 列表
     * 参数说明 Standard
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="查询能力建设 列表")
    @UBA(module = "成果管理", action = "查询能力建设 列表", channel = OperatorType.Page)
    @RequestMapping(value = "/listPage", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult listPage(HttpServletRequest request, @RequestBody Capacity req) {
        MyPageResult<Capacity> result = capacityService.queryCapacityPageList(req);
        return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,result);
    }

    /**********************************
     * 用途说明: 删除能力建设
     * 参数说明 subject
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="删除能力建设")
    @UBA(module = "成果管理", action = "删除能力建设", channel = OperatorType.Button)
    @RequestMapping(value = "/delByIds", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult delByIds(HttpServletRequest request, @RequestBody List<String> ids) {
        if (capacityService.delByIds(ids))
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource);
        else
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,"删除失败!");
    }
    /**********************************
     * 用途说明: 调试科研创新管理===成果管理===能力建设====导出Excel
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "调试科研创新管理-成果管理-能力建设--导出Excel")
    @UBA(module = "调试科研创新管理", action = "能力建设导出 Excel", channel = OperatorType.Button)
    @RequestMapping(value = "/exportExcel", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends Capacity> Object exportExcel(HttpServletRequest request, @RequestBody T reqEntity) {
        AjaxResult ajaxResult = null;

        try {
            CreateFileInfo createFileInfo = capacityService.exportExcel(reqEntity);
            ajaxResult = AjaxResultUtil.createSuccessMessage(createFileInfo);
        } catch (Exception e) {
            LOGGER.error("exportExcel.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }
    /**********************************
     * 用途说明: 调试科研创新管理-成果管理-能力建设---导入Excel
     * 参数说明: HttpServletRequest request
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "调试科研创新管理-成果管理-能力建设---导入Excel")
    @UBA(module = "调试科研创新管理", action = "能力建设导入 Excel", channel = OperatorType.Button)
    @RequestMapping(value = "/importFromExcel", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends Capacity> Object importFromExcel(MultipartHttpServletRequest request) {
        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
            UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

            MultipartFile reqFile = request.getFile("file");

            ajaxResult = capacityService.importFromExcel(loginUserInfo, reqLocale, reqFile);
        } catch (Exception e) {
            LOGGER.error("importFromExcel.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }
}
